//
//  GraphicViewController.m
//  Alltelevisionnews
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "GraphicViewController.h"

@interface GraphicViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
@property(nonatomic, strong)UITableView *myTbaleView;
@property(nonatomic, strong)NSMutableArray *arr;
@property(nonatomic, strong)MBProgressHUD *hud;
@property(nonatomic, strong)UISegmentedControl *seg;
@property(nonatomic, strong)UIView *tableViewhead;
@property(nonatomic, strong)UIView *footView;
@property(nonatomic, strong)UISearchBar *search;
@property(nonatomic, strong)NSMutableArray *deepArr;
@property(nonatomic, strong)NSString *str;
@end

@implementation GraphicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    UIButton *menuBtu = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtu.frame = CGRectMake(0, 0, 20, 18);
    [menuBtu setBackgroundImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
    [menuBtu addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtu];

    /////segment
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"主笔", @"深度"]];
    self.seg.frame = CGRectMake(110, 20, 150, 30);
    self.navigationItem.titleView = self.seg;
    self.seg.tintColor = [UIColor blueColor]; //边框和字体颜色

    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    self.seg.selectedSegmentIndex = 0;
    
    self.myTbaleView = [[UITableView alloc] init];
    [self.view addSubview:self.myTbaleView];
    self.myTbaleView.rowHeight = 110;
    [self.myTbaleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    self.tableViewhead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.myTbaleView.frame.size.width, 40)];
    self.tableViewhead.backgroundColor = [UIColor grayColor];
    self.myTbaleView.tableHeaderView = self.tableViewhead;
    
    self.search = [[UISearchBar alloc] init];
    self.search.placeholder = @"搜索:新闻搜索";
    [self.tableViewhead addSubview:self.search];
    [self.search mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableViewhead).with.offset(0);
        make.bottom.equalTo(self.tableViewhead).with.offset(0);
        make.width.mas_equalTo(self.view);
    }];
    self.search.delegate = self;

    self.myTbaleView.dataSource = self;
    self.myTbaleView.delegate = self;
    [self createData];
    
    if ([ZDQSingleton shareSingleton].isLongin == YES) {
        self.myTbaleView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
    } else {
        
        self.myTbaleView.backgroundColor = [UIColor whiteColor];
    }

    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"正在加载";
    
}

#pragma mark button的点击方法
- (void)openOrCloseLeftList {
    
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (tempAppDelegate.leftslideVC.closed) {
        
        [tempAppDelegate.leftslideVC openLeftView];
        
    } else {
        
        [tempAppDelegate.leftslideVC closeLeftView];
        
    }
    
}



// seg的点击
- (void)segAction:(UISegmentedControl *)seg {
    
    switch (seg.selectedSegmentIndex) {
        case 0:
            self.view.backgroundColor = [UIColor redColor];
            [self createData];
            break;
        case 1:
            self.view.backgroundColor = [UIColor blueColor];
            [self createMama];
        default:
            break;
    }
}
//// 主笔的网络请求
- (void)createData {
    
    [SLAFNetWorkTool cacheGetWithURL:@"http://api.3g.ifeng.com/zbpd?gv=4.6.5&av=0&proid=ifengnews&os=ios_8.4.1&vt=5&screen=640x960&publishid=4002&uid=00843d54db6759a6011ed5becdd59b2e5420d3fd" cookie:nil success:^(id responseObject) {
        
        self.arr = [NSMutableArray array];
        self.arr = [GraphicModel baseModelByArr:responseObject[0][@"items"]];
//        NSLog(@"%@", [self.arr[0] title]);
        [self.myTbaleView reloadData];
        self.hud.hidden = YES;
    } failure:^(NSError *error) {
    }];
}

/////// 深度的网络请求
- (void)createMama {
   
    [SLAFNetWorkTool cacheGetWithURL:@"http://api.iclient.ifeng.com/clientShortNews?type=depth&gv=4.6.5&av=0&proid=ifengnews&os=ios_8.4.1&vt=5&screen=640x960&publishid=4002&uid=00843d54db6759a6011ed5becdd59b2e5420d3fd" cookie:nil success:^(id responseObject) {
      self.deepArr = [NSMutableArray array];
        self.deepArr = [deepModel baseModelByArr:responseObject[0][@"item"]];
            [self.myTbaleView reloadData];
        self.hud.hidden = YES;
    } failure:^(NSError *error) {
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.seg.selectedSegmentIndex == 0) {
        return self.arr.count;
    }
    else {
        return self.deepArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.seg.selectedSegmentIndex == 0) {
        static NSString *remmm = @"reuse";
        GraphicTableCell *cell = [tableView dequeueReusableCellWithIdentifier:remmm];
        if (!cell) {
            cell = [[GraphicTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:remmm];
        }
        cell.backgroundColor = [UIColor clearColor];
        GraphicModel *graphic = self.arr[indexPath.row];
        [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:graphic.img] placeholderImage:[UIImage imageNamed:@"pac.png"]];;
        cell.titleLabel.text = graphic.title;
        cell.talkLabel.textColor = [UIColor grayColor];
        cell.talkLabel.text = graphic.introduction;
        
        if ([ZDQSingleton shareSingleton].isLongin == YES) {
            cell.titleLabel.textColor = [UIColor whiteColor];
            cell.talkLabel.textColor = [UIColor whiteColor];
            cell.leftImage.alpha = 0.4;
        } else {
            
            cell.titleLabel.textColor = [UIColor blackColor];
            cell.talkLabel.textColor = [UIColor grayColor];
            cell.leftImage.alpha = 1;
            
        }
        
        return cell;
    }
    else {
        deepModel *deep = self.deepArr[indexPath.row];
        if (![deep.thumbnail isEqualToString:@""]) {
            static NSString *reuse = @"raaaa";
            TopicPageCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
            if (!cell) {
                cell = [[TopicPageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
            }
            NSString *timeStr = [deep.updateTime substringWithRange:NSMakeRange(11, 5)];
//            NSLog(@"%@", timeStr);
            cell.backgroundColor = [UIColor clearColor];
            cell.topicLabel.text = timeStr;
            [cell.picImage sd_setImageWithURL:[NSURL URLWithString:deep.thumbnail] placeholderImage:[UIImage imageNamed:@"pac.png"]];;
            cell.titleLabel.text = deep.title;
            
            if ([ZDQSingleton shareSingleton].isLongin == YES) {
                cell.topicLabel.textColor = [UIColor whiteColor];
                cell.titleLabel.textColor = [UIColor whiteColor];
                cell.picImage.alpha = 0.4;
            } else {
                
                cell.titleLabel.textColor = [UIColor blackColor];
                cell.topicLabel.textColor = [UIColor blackColor];
                cell.picImage.alpha = 1;
            }
            
            return cell;

        }
        else {
            static NSString *reuse = @"ramam";
            DetaisViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
            if (!cell) {
                cell = [[DetaisViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
            }
            cell.backgroundColor = [UIColor clearColor];
            cell.titleLabel.text = deep.title;
            cell.titleLabel.numberOfLines = 0;
            cell.talkLabel.textColor = [UIColor grayColor];
            cell.talkLabel.text = [NSString stringWithFormat:@"%@ 阅", deep.commentsall];
            NSString *timeStr = [deep.updateTime substringWithRange:NSMakeRange(11, 5)];
            cell.timeLabel.text = timeStr;

            if ([ZDQSingleton shareSingleton].isLongin == YES) {
                cell.titleLabel.textColor = [UIColor whiteColor];
                cell.talkLabel.textColor = [UIColor whiteColor];
                cell.timeLabel.textColor = [UIColor whiteColor];
            } else {
                
                cell.titleLabel.textColor = [UIColor blackColor];
                cell.talkLabel.textColor = [UIColor grayColor];
                cell.timeLabel.textColor = [UIColor blackColor];
                
            }
            
            return cell;
        }
    }
}

#pragma mark 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.seg.selectedSegmentIndex == 0) {
        
        NSArray *arr = @[@"25572", @"25596", @"27456", @"25570", @"27186", @"26884", @"27132", @"25536"];
        GrapDetaisViewController *pancelVC = [[GrapDetaisViewController alloc] init];
        [self.navigationController pushViewController:pancelVC animated:YES];
        pancelVC.strId = arr[indexPath.row];
        pancelVC.titlestr = [self.arr[indexPath.row] title];
//        NSLog(@"----%@", arr[indexPath.row]);
    }
    else {
        
        DetaisWebViewController *webVC = [[DetaisWebViewController alloc] init];
        [self.navigationController pushViewController:webVC animated:YES];
        webVC.strId = [self.deepArr[indexPath.row] commentsUrl];
    }
}

#pragma mark 搜索
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self.search resignFirstResponder];
}

//开始编辑
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    // 转码 把汉字转换成编码
    
}
// 实时搜索
- (void)searchBar:(UISearchBar *)searchBar textDidChange:
(NSString *)searchText {
    
}
// 点确定 点搜索走它
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    searchViewController *searchVC = [[searchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
    searchVC.strNews = self.search.text;
    [self.search resignFirstResponder];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
